package com.aloda.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.aloda.constant.PageConstant;
import com.aloda.dto.PageEditDTO;
import com.aloda.entity.PageContent;
import com.aloda.mapper.PageContentMapper;
import com.aloda.service.PageContentService;
import com.aloda.vo.PageContentVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
* @author aloda
* @description 针对表【page_content(页面内容表)】的数据库操作Service实现
* @createDate 2024-12-30 17:17:58
*/
@Service
public class PageContentServiceImpl extends ServiceImpl<PageContentMapper, PageContent> implements PageContentService {

    @Resource
    private PageContentMapper pageContentMapper;

    /**
     * 保存关于页面内容
     *
     * @param pageEditDTO
     */
    @Override
    public void saveAboutPage(PageEditDTO pageEditDTO) {
        LambdaQueryWrapper<PageContent> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PageContent::getNum,PageConstant.ABOUT_PAGE_NUM);
        PageContent pageContent = pageContentMapper.selectOne(wrapper);
        if (BeanUtil.isEmpty(pageContent)) {
            PageContent pageContent1 = new PageContent();
            pageContent1.setNum(PageConstant.ABOUT_PAGE_NUM);
            pageContent1.setContent(pageEditDTO.getContent());
            pageContent1.setCreateTime(LocalDateTime.now());
            pageContent1.setUpdateTime(LocalDateTime.now());
            pageContentMapper.insert(pageContent1);
            return;
        }
        pageContent.setContent(pageEditDTO.getContent());
        pageContent.setUpdateTime(LocalDateTime.now());
        pageContentMapper.updateById(pageContent);
    }

    /**
     * 获取关于页面内容
     *
     * @return
     */
    @Override
    public PageContentVO getAboutPage() {
        LambdaQueryWrapper<PageContent> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PageContent::getNum,PageConstant.ABOUT_PAGE_NUM);
        PageContent pageContent = this.getOne(wrapper);
        return BeanUtil.copyProperties(pageContent, PageContentVO.class);
    }
}




